package com.yisu.auction.manager.core.service.impl;

import com.yisu.auction.ewf.entity.BiDataCollect;
import com.yisu.auction.ewf.entity.OfferPriceLogArchive;
import com.yisu.auction.manager.core.mapper.BiDataCollectMapper;
import com.yisu.auction.manager.core.mapper.MemberMapper;
import com.yisu.auction.manager.core.mapper.OfferPriceLogMapper;
import com.yisu.auction.manager.core.mapper.RechargeMapper;
import com.yisu.auction.manager.core.service.IDataCollectService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/9/28
 */
@Service("IDataCollectService")
public class DataCollectServiceImpl implements IDataCollectService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private OfferPriceLogMapper offerPriceLogMapper;
    @Resource
    private BiDataCollectMapper biDataCollectMapper;
    @Resource
    private RechargeMapper rechargeMapper;

    /**
     * author: 赵杰
     * Business Description：添加数据汇总
     *
     * @return
     */
    @Override
    public boolean addDataCollect(Integer days) {
        for (int i = 1; i <= days; i++) {
            BiDataCollect biDataCollect = new BiDataCollect();
            Calendar date = Calendar.getInstance();
            date.setTime(new Date());
            date.add(Calendar.DATE, -i);
            Date thisDate = date.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = simpleDateFormat.format(thisDate);
            //获取当日注册用户数
            Integer currentDayRegisterCount = memberMapper.getMemberCountByDate(dateStr);
            biDataCollect.setCreateTime(new Date());
            biDataCollect.setCollectDate(thisDate);
            biDataCollect.setMemberNewCount(currentDayRegisterCount);
            //获取用户总数
            biDataCollect.setMemberTotalCount(memberMapper.getMemberCount());
            //今日竞拍用户数
            biDataCollect.setTodayAuctionMember(offerPriceLogMapper.getCurrentDayAuctionMemberCount(dateStr));
            //今日竞拍新用户数
            biDataCollect.setTodayAuctionNewMember(offerPriceLogMapper.getTodayAuctionNewMemberByDate(dateStr));
            //获取今日充值总金额
            biDataCollect.setTodayRechargeMoney(rechargeMapper.getTodayRechargeMoney(dateStr));
            //今日消费总金额
            biDataCollect.setTodayBuyMoney(offerPriceLogMapper.getCurrentDayBuyMoney(dateStr));
            //今日新用户消费总金额
            biDataCollect.setNewMemberBuyMoney(offerPriceLogMapper.getCurrentDayBuyMoneyForNew(dateStr));
            //获取累计消费金额
            biDataCollect.setTotalBuyMoney(memberMapper.getAccumulativeBuyMoney());
            if (biDataCollect.getMemberNewCount() > 0) {
                biDataCollect.setNewMemberKdj(biDataCollect.getNewMemberBuyMoney().divide(BigDecimal.valueOf(biDataCollect.getMemberNewCount()),2));
            } else {
                biDataCollect.setNewMemberKdj(BigDecimal.valueOf(0));
            }
            biDataCollectMapper.insertSelective(biDataCollect);
        }
        return true;
    }

    /**
     * author: 赵杰
     * Business Description：获取BI数据报表-一周数据
     *
     * @return
     */
    @Override
    public List<BiDataCollect> getBiDataCollect() {
        List<BiDataCollect> biDataCollects = biDataCollectMapper.getBiDataCollect();
        BiDataCollect biDataCollect = biDataCollects.get(biDataCollects.size()-1);
        if (!CollectionUtils.isEmpty(biDataCollects)) {
            if (biDataCollects.size() < 7) {
                int surplusNum = 7 - biDataCollects.size();
                for (int i = 1; i <= surplusNum; i++) {
                    Calendar date = Calendar.getInstance();
                    date.setTime(biDataCollect.getCollectDate());
                    date.add(Calendar.DATE, -i);
                    Date thisDate = date.getTime();
                    BiDataCollect tempModel = new BiDataCollect();
                    tempModel.setCreateTime(new Date());
                    tempModel.setCollectDate(thisDate);
                    tempModel.setNewMemberKdj(BigDecimal.valueOf(0));
                    tempModel.setTotalBuyMoney(BigDecimal.valueOf(0));
                    tempModel.setNewMemberBuyMoney(BigDecimal.valueOf(0));
                    tempModel.setTodayBuyMoney(BigDecimal.valueOf(0));
                    tempModel.setTodayAuctionNewMember(0);
                    tempModel.setTodayAuctionMember(0);
                    tempModel.setMemberTotalCount(0);
                    biDataCollects.add(tempModel);
                }
            }
        }
        return biDataCollects;
    }
}
